<template>
  <a-modal :title="title" :width="width" :visible="visible" :confirmLoading="confirmLoading" :cancelText="$t('关闭')" :okText="$t('保存')" @cancel="handleCancel" @ok="displaySet">
    <a-spin :spinning="confirmLoading">
      <div style="padding: 14px 0; line-height: 40px">
        <div style="margin-bottom: 10px">
          <span style="color: orange; line-height: 41px; margin-bottom: 10px">
            {{ $t('（启用数据隐藏功能后，在页面上按Ctrl+K查看真实账目）') }}
          </span>
        </div>
        <div style="margin-left: 20px; margin-bottom: 10px">
          <a-checkbox v-model="dataDisplayRatio.ivalues">{{ $t('数值显示比例') }}：</a-checkbox>
          <a-input-number
            v-model="dataDisplayRatio.svalue"
            style="width: 130px; margin-left: 10px"
            :disabled="!dataDisplayRatio.ivalues"
            :precision="0"
            :min="1"
            :max="100"
          />
          <span style="margin-left: 10px">%</span>
        </div>
        <div style="margin-left: 20px; margin-bottom: 10px">
          <a-checkbox v-model="recordDisplayNum.ivalues">{{ $t('记录显示条数') }}：</a-checkbox>
          <a-input-number
            v-model="recordDisplayNum.svalue"
            style="width: 130px; margin-left: 10px"
            :disabled="!recordDisplayNum.ivalues"
            :precision="0"
            :min="0"
            :max="99999"
          />
          <span style="margin-left: 10px">{{ $t('条') }}</span>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { getAction, httpAction } from '@/api/manage'

export default {
  name: 'DataHideModal',
  data() {
    return {
      title: this.$t('数据隐藏'),
      width: 600,
      visible: false,
      confirmLoading: false,
      dataDisplayRatio: {
        name: 'dataDisplayRatio',
        ivalue: 0,
        ivalues: false,
        svalue: '',
        comment: '数值显示比例',
      },
      recordDisplayNum: {
        name: 'recordDisplayNum',
        ivalue: 0,
        ivalues: false,
        svalue: '',
        comment: '记录显示条数',
      },
    }
  },
  methods: {
    show() {
      this.visible = true
      this.getParameter('dataDisplayRatio')
      this.getParameter('recordDisplayNum')
    },
    displaySet() {
      if (
        (this.dataDisplayRatio.ivalues === true) &&
        (this.dataDisplayRatio.svalue == undefined || this.dataDisplayRatio.svalue == '')
      ) {
        this.$message.warning(this.$t('数值显示比例未设置'))
        return
      }
      if (
        (this.recordDisplayNum.ivalues === true) &&
        (this.recordDisplayNum.svalue == undefined || this.recordDisplayNum.svalue == '')
      ) {
        this.$message.warning(this.$t('记录显示条数未设置'))
        return
      }
      this.parameterChange('dataDisplayRatio')
      this.parameterChange('recordDisplayNum')
    },
    getParameter(name) {
      getAction('/bis/bisSysParameter/list', { name: name }).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            let dtl= res.result.records[0]
            dtl.ivalues=dtl.ivalue?true:false
            if (name === 'dataDisplayRatio') {
              this.dataDisplayRatio = dtl
            }
            if (name === 'recordDisplayNum') {
              this.recordDisplayNum = dtl
            }
          }
        }
      })
    },
    parameterChange(name) {
      let querydata = {}
      let url = ''
      let method = ''
      if (
        (name === 'dataDisplayRatio' && this.dataDisplayRatio.id) ||
        (name === 'recordDisplayNum' && this.recordDisplayNum.id)
      ) {
        url = '/bis/bisSysParameter/edit'
        method = 'put'
      } else {
        url = '/bis/bisSysParameter/add'
        method = 'post'
      }
      if (name === 'dataDisplayRatio') {
        if (this.dataDisplayRatio.ivalues === true) {
          this.dataDisplayRatio.ivalue = 1
        } else {
          this.dataDisplayRatio.ivalue = 0
        }
        querydata = Object.assign({}, this.dataDisplayRatio)
      }
      if (name === 'recordDisplayNum') {
        if (this.recordDisplayNum.ivalues === true) {
          this.recordDisplayNum.ivalue = 1
        } else {
          this.recordDisplayNum.ivalue = 0
        }
        querydata = Object.assign({}, this.recordDisplayNum)
      }
      httpAction(url, querydata, method).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          if (name === 'dataDisplayRatio') {
            this.getParameter('dataDisplayRatio')
          }
          if (name === 'recordDisplayNum') {
            this.getParameter('recordDisplayNum')
          }
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    handleCancel() {
      this.close()
    },
    close() {
      this.visible = false
    },
  },
}
</script>

<style scoped>
</style>

